# -*- coding: utf-8 -*-¨

import helpers.dbmanager
import types

class BooksModel():
    db = None
    books = {}
    tags = {}

    def __init__(self):
        pass

    def synchronize(self):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        self.books.clear()
        tmpBooks = self.db.listBooks()
        for book in tmpBooks:
            for name, value in book.iteritems():
                if isinstance(value, types.NoneType) : book[name] = ""

            self.books[book["id"]] = book;

        self.updateTagList()

    def updateTagList(self):
        self.tags.clear()
        for book in self.books.itervalues():
            bookTags = book["tags"].split(",")
            for tag in bookTags:
                if tag not in self.tags:
                    self.tags[tag] = []
                self.tags[tag].append(book["id"])

    def getBooksList(self):
        return self.books

    def getTagsList(self):
        return self.tags

    def saveBook(self, book):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        if "name" not in book or len(book["name"]) == 0:
            return False

        if "id" in book and book["id"] > 0:
            self.db.updateBook(book["id"], book["name"], book["author"], book["isbn"], book["descr"], book["genre"], book["tags"])
            self.books[book["id"]] = book;
        else:
            id = self.db.createNewBook(book["name"], book["author"], book["isbn"], book["descr"], book["genre"], book["tags"])
            if id is -1:
                return False
            book["id"] = id
            self.books[id] = book

        self.updateTagList()
        return True

    def deleteBook(self, bookId):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        if bookId in self.books:
            del self.books[bookId]
            return self.db.deleteBook(bookId)
        else:
            return False

    def getBookById(self, bookId):
        if bookId in self.books:
            return self.books[bookId]
        else:
            return None
        
    def getBooksByIds(self, ids):
        ret = {}
        for id in ids:
            book = self.getBookById(id)
            if book is not None:
                ret[id] = book
        return ret

    def getBooksByTag(self, tag):
        if tag not in self.tags:
            return {}
        ret = {}
        for bookId in self.tags[tag]:
            ret[bookId] = self.books[bookId]

        return ret

    def getBooksByTags(self, tags):
        ret = {}
        for tag in tags:
            ret.update(self.getBooksByTag(tag))
        return ret

    def getBookExists(self, bookId):
        return bookId in self.books

    def getBookName(self, bookId):
        return self.books[bookId]["name"]

    def getDefaultBookId(self):
        self.db = helpers.dbmanager.DBManager.getInstance().get()
        books = self.db.listBooks(u"- nezařazené -")
        if len(books) is 0:
            return None
        return books[0]["id"]